package tech.yunjing.train.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.yunjing.train.common.Constants;
import tech.yunjing.train.dao.AccountDao;
import tech.yunjing.train.dao.AccountDetailDao;
import tech.yunjing.train.dao.ReceiveRedPacketRecordRepository;
import tech.yunjing.train.dao.RedPacketDao;
import tech.yunjing.train.entity.Account;
import tech.yunjing.train.entity.AccountDetail;
import tech.yunjing.train.entity.ReceiveRedPacketRecord;
import tech.yunjing.train.entity.RedPacket;
import tech.yunjing.train.service.AccountDetailService;
import tech.yunjing.train.service.ReceiveRedPacketRecordService;
import tech.yunjing.train.service.exception.AccountNameException;

import java.util.List;

/**
 * 账户明细接口实现类，具体业务实现
 * Created by ZhangFan on 2017/6/9.
 */
@Service
public class AccountDetailServiceImpl implements AccountDetailService {

    @Autowired
    AccountDetailDao accountDetailDao;
    @Autowired
    AccountDao accountDao;
    @Autowired
    ReceiveRedPacketRecordService receiveRedPacketRecordService;
    @Autowired
    ReceiveRedPacketRecordRepository receiveRedPacketRecordRepository;
    @Autowired
    RedPacketDao redPacketDao;

    /**
     * 新增账户明细
     *
     * @param accountDetail 明细对象
     * @return 新增记录条数
     */
    @Override
    public int addAccountDetail(AccountDetail accountDetail) throws AccountNameException {
        //增加明细前根据账户名判断账户是否存在
        Account account = accountDao.findByAccountName(accountDetail.getAccountName());
        if (account == null) {
            throw new AccountNameException("请重新登录");
        }
        return accountDetailDao.addAccountDetail(accountDetail);
    }

    /**
     * 根据账户名查询账户明细
     *
     * @param accountName 账户名
     * @return List<AccountDetail> 账户的所有明细
     */
    @Override
    public Account findAccountDetailsByAccountName(String accountName) {
        Account account = accountDao.findByAccountName(accountName);
        //获取抢红包的明细
        List<ReceiveRedPacketRecord> receiveList = receiveRedPacketRecordRepository.findByReceiveAccountAndFindState(accountName, Constants.FIND_STATE_NORMAL);
        receiveRedPacketRecordService.saveAccountDetail(receiveList,accountName);
        //获取发红包的明细
        List<RedPacket> outList = redPacketDao.findAllByUserAccount(accountName);
        for (RedPacket outRecord : outList) {
            AccountDetail accountDetail = new AccountDetail();
            accountDetail.setAccountName(accountName);
            accountDetail.setHappendMoney(-outRecord.getRedPacketAmount());
            if (outRecord.getUpdateDate() != null) {
                accountDetail.setHappendTime(outRecord.getUpdateDate());
            } else {
                accountDetail.setHappendTime(outRecord.getCreateDate());
            }
            accountDetailDao.addAccountDetail(accountDetail);
        }
        List<AccountDetail> accountDetails = accountDetailDao.getAccountDetailsByAccountName(accountName);
        account.setAccountDetail(accountDetails);
        return account;
    }

    /**
     *
     * @param accountId 账户Id
     * @return  List<AccountDetail> 账户的所有明细
     */
    @Override
    public Account findAccountDetailsByAccountId(Integer accountId) {
        Account account = accountDao.findByAccountId(accountId);
        //获取抢红包的明细
        List<ReceiveRedPacketRecord> receiveList = receiveRedPacketRecordRepository.findByReceiveAccountAndFindState(account.getAccountName(), Constants.FIND_STATE_NORMAL);
        receiveRedPacketRecordService.saveAccountDetail(receiveList,account.getAccountName());
        //获取发红包的明细
        List<RedPacket> outList = redPacketDao.findAllByUserAccount(account.getAccountName());
        for (RedPacket outRecord : outList) {
            AccountDetail accountDetail = new AccountDetail();
            accountDetail.setAccountName(account.getAccountName());
            accountDetail.setHappendMoney(-outRecord.getRedPacketAmount());
            if (outRecord.getUpdateDate() != null) {
                accountDetail.setHappendTime(outRecord.getUpdateDate());
            } else {
                accountDetail.setHappendTime(outRecord.getCreateDate());
            }
            accountDetailDao.addAccountDetail(accountDetail);
        }
        List<AccountDetail> accountDetails = accountDetailDao.getAccountDetailsByAccountId(accountId);
        account.setAccountDetail(accountDetails);
        return account;
    }

}
